import { loginWechat, LoginByToken ,GetUserInfo} from '@/api/AuthApi.js'

const state = {
  user: 'null',
  token: 'null',
}
const mutations = {
  SET_INFO: (state, data) => { state.user = data },
  SET_TOKEN: (state, data) => {state.token = data },
}

const actions = {
	setRelUserInfo({commit},params){
		commit('SET_INFO', params);
	},
	async loginWechat({commit}, params) {
		let res = await loginWechat(params);
		console.log("loginWechat返回值")
		console.log(res)
		if(res.code==100200) {
			SyncUserInfo(commit, res.data);
		}
		return res;
	},
	async LoginByToken({commit},params) {
		let res = await LoginByToken(params);
		if(res.code===100200) {
			console.log("LoginByToken返回值")
			SyncUserInfo(commit, res.data);
		}
		return res;
	},
	async GetUserInfo({commit},params) {
		let res = await GetUserInfo(params)
		if(res.code===100200) {
			console.log("GetUserInfo返回值")
			SyncUserInfo(commit, res.data);
		}
		return res;
	},
	Logout({commit, state}) {
		uni.removeStorageSync('token');
		commit('SET_TOKEN', '');
		commit('SET_INFO', null);
	}
}
	
	
//同步用户信息
const SyncUserInfo = function(commit, userInfo) {
	commit('SET_TOKEN', userInfo.token);
	commit('SET_INFO', userInfo);
	
	uni.setStorageSync("token", userInfo.token);
	uni.setStorageSync("userid", userInfo.id);
}

export default {
  state,
  mutations,
  actions
}
